package com.ruoyi.htzj.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.htzj.domain.HtzjStreets;
import com.ruoyi.htzj.domain.bo.HtzjStreetsBo;
import com.ruoyi.htzj.service.HtzjStreetsService;
import com.ruoyi.htzj.mapper.HtzjStreetsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xq
 * @description 针对表【htzj_streets(街道信息表)】的数据库操作Service实现
 * @createDate 2025-03-24 03:09:17
 */
@Service
public class HtzjStreetsServiceImpl extends ServiceImpl<HtzjStreetsMapper, HtzjStreets>
    implements HtzjStreetsService {

    @Override
    public TableDataInfo<HtzjStreets> queryPageList(HtzjStreets streets, PageQuery pageQuery) {
        // 创建分页对象
        Page<HtzjStreets> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        // 创建查询条件
        LambdaQueryWrapper<HtzjStreets> queryWrapper = buildQueryWrapper(streets);
        queryWrapper.like(StrUtil.isNotBlank(streets.getStreetAddress()), HtzjStreets::getStreetAddress, streets.getStreetAddress());
        queryWrapper.orderByDesc(HtzjStreets::getStreetId);
        // 执行分页查询
        Page<HtzjStreets> resultPage = this.page(page, queryWrapper);
        // 封装分页结果
        return TableDataInfo.build(resultPage);
    }

    @Override
    public R<List<HtzjStreets>> queryList(HtzjStreets streets) {
        // 创建查询条件
        LambdaQueryWrapper<HtzjStreets> queryWrapper = buildQueryWrapper(streets);
        // 执行查询
        List<HtzjStreets> list = this.list(queryWrapper);
        // 返回查询结果
        return R.ok(list);
    }

    @Override
    public HtzjStreets queryById(Long id) {
        // 根据ID查询记录
        return this.getById(id);
    }

    @Override
    public void insertStreets(HtzjStreetsBo htzjStreetsBo) {
        // 插入新记录
        HtzjStreets streets = new HtzjStreets();
        BeanUtils.copyProperties(htzjStreetsBo, streets);
        this.save(streets);
    }

    @Override
    public void updateStreets(HtzjStreetsBo htzjStreetsBo) {
        // 根据ID更新记录
        HtzjStreets streets = new HtzjStreets();
        BeanUtils.copyProperties(htzjStreetsBo, streets);
        streets.setStreetId(htzjStreetsBo.getStreetId());
        this.updateById(streets);
    }

    @Override
    public void deleteStreetsByIds(Long[] streetIds) {
        // 根据ID数组批量删除记录
        this.removeByIds(List.of(streetIds));
    }

    /**
     * 构建查询条件
     *
     * @param streets 查询条件对象
     * @return 查询条件包装器
     */
    private LambdaQueryWrapper<HtzjStreets> buildQueryWrapper(HtzjStreets streets) {
        LambdaQueryWrapper<HtzjStreets> queryWrapper = new LambdaQueryWrapper<>();
        // 这里可以根据 HtzjStreets 类的属性添加具体的查询条件
        // 例如：
        // if (StringUtils.isNotBlank(streets.getName())) {
        //     queryWrapper.like(HtzjStreets::getName, streets.getName());
        // }
        return queryWrapper;
    }
}




